﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.Text;
using WooCoo.DataPortal;
using WooCoo.DataPortal.Server.Hosts.WcfChannel;

namespace WooCoo.Client.Wcf
{
    /// <summary>
    /// 客户端消息检查器
    /// </summary>
    internal class ClientMessageInspector : IClientMessageInspector
    {

        #region IClientMessageInspector 成员

        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            if (reply.Headers.Action == ServiceExceptionDetail.FaultAction)
            {
                MessageFault fault = MessageFault.CreateFault(reply, int.MaxValue);
                if (fault.Code.SubCode.Name == ServiceExceptionDetail.FaultSubCodeName &&
                    fault.Code.SubCode.Namespace == ServiceExceptionDetail.FaultSubCodeNamespace)
                {
                    FaultException<ServiceExceptionDetail> exception = (FaultException<ServiceExceptionDetail>)FaultException.CreateFault(fault, typeof(ServiceExceptionDetail));
                    throw ServiceExceptionDetail.GetException(exception.Detail);
                }
            }
        }

        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            ////登陆方法名需求特殊处理。
            //if (Wcdf.ApplicationContext.AuthenticationType != "Windows")
            //{
            //    UsernameCriteria uc = new UsernameCriteria();
            //    if (Wcdf.ApplicationContext.User is Wcdf.Security.UnauthenticatedPrincipal)
            //    {
            //        uc.IsFirstAuthorization = true;
            //        //如果是未验证的权限，则在请求前加消息头以辨别身份。
            //        //var msg = new DataPortalContext(Wcdf.ApplicationContext.User, true);                    
            //        var id = Wcdf.ApplicationContext.User.Identity as Wcdf.Security.UnauthenticatedIdentity;
            //        uc.UserName = id.UserName;
            //        uc.Password = id.Password;
            //    }
            //    else
            //    {
            //        var id = Wcdf.ApplicationContext.User.Identity as Wcdf.Security.WcdfIdentity;
            //        uc.UserName = id.UserInfo.UserID;
            //        uc.Password = id.UserInfo.Password;
            //    }
            //    MessageHeader<UsernameCriteria> uinfo = new MessageHeader<UsernameCriteria>(uc);
            //    request.Headers.Add(uinfo.GetUntypedHeader(DataPortalContext.LoginInfo, DataPortalContext.NameSpace));
            //}  
            var msg = new DataPortalContext();
            MessageHeader<DataPortalContext> header = new MessageHeader<DataPortalContext>(msg);
            //request.Headers.Add(header.GetUntypedHeader(DataPortalContext.Name, DataPortalContext.NameSpace));
            return null;
        }

        #endregion

        private static System.Security.Principal.IPrincipal GetPrincipal()
        {
            if (ApplicationContext.AuthenticationType == "Windows")
            {
                // 集成身份验证时，不传递身份凭据
                return null;
            }
            else
            {
                // we assume using the CSLA framework security
                return ApplicationContext.User;
            }
        }
    }
}
